<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<style type="text/css">
	body{overflow:auto;}
  thead tr {background-color: ActiveCaption; color: CaptionText;}
  th, td {vertical-align: top; font-family: "宋体", Arial, Helvetica, sans-serif; font-size: 9pt; padding: 3px; }
  table, td {border: 1px solid silver;}
  table {border-collapse: collapse;}
  thead .col0 {width: 20%;}
  thead .col1 {width: 75%;}
	td.col0{font-size:18px;font-weight:bold;color:#f00;}
</style>
<body class="white-bg">
    <table>
      <thead>
        <tr>
          <th class="col0">章节</th>
          <th class="col1">内容</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="col0">3.4 系统日志</td>
          <td class="col1"><div style="text-indent: 28px;"><span style="font-family:宋体;"><span style="font-size: 18px;"></span></span><p><span style="font-family:宋体;">系统日志是应用软件中不可缺少的部分，通过日志，可以分析判断项目运行过程中在什么位置，出现了什么问题。</span></p><p>Apache<span style="font-family:宋体;">的开源项目</span><strong>Log4j</strong><span style="font-family:宋体;">是一个功能强大的日志组件，通过使用</span>Log4j<span style="font-family:宋体;">，可以控制日志信息输出到控制台</span>console<span style="font-family:宋体;">、文件、</span>GUI<span style="font-family:宋体;">组件，甚至是套接口服务器、</span>NT<span style="font-family:宋体;">的事件记录器等；也可以控制每一条日志的输出格式；通过定义每一条日志信息的级别，能够更加细致地控制日志的生成过程。通过一个配置文件就可以灵活地进行配置，而不需要修改应用的代码。</span></p><p><span style="font-family:宋体;">使用</span>log4j<span style="font-family:宋体;">实现系统日志的方法非常简单。</span></p><p>(1)<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family:宋体;">添加</span>log4j-xxx.jar<span style="font-family:宋体;">到</span>lib<span style="font-family:宋体;">下，已经在</span>3.2.2<span style="font-family:宋体;">节完成。</span></p><p>(2)<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family:宋体;">在</span>conf<span style="font-family:宋体;">目录中新建日志配置文件</span>log4j.properties<span style="font-family:宋体;">，内容可以参照网上的详细配置，也可以采用下面的简单配置：</span></p><p><span style="color:#990055;">log4j.rootLogger</span><span style="color:#444444;background:#F0F0F0;">=</span><span style="color:#0077AA;">debug,Console</span></p><p><span style="color:#990055;">log4j.appender.Console</span><span style="color:#444444;background:#F0F0F0;">=</span><span style="color:#0077AA;">org.apache.log4j.ConsoleAppender</span></p><p><span style="color:#990055;">log4j.appender.Console.layout</span><span style="color:#444444;background:#F0F0F0;">=</span><span style="color:#0077AA;">org.apache.log4j.PatternLayout</span></p><p><span style="color:#990055;">log4j.appender.Console.layout.ConversionPattern</span><span style="color:#444444;background:#F0F0F0;">=</span><span style="color:#0077AA;">%d %l %-5p -%m%n</span></p><p><span style="font-family:Wingdings;">l<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>log4j.rootLogger<span style="font-family:宋体;">配置根</span>Logger</p><p><span style="font-family:宋体;">包括选择日志输出级别（有</span>5<span style="font-family:宋体;">个级别可供选择：</span>FATAL<span style="font-family:宋体;">、</span>ERROR<span style="font-family:宋体;">、</span>WARN<span style="font-family:宋体;">、</span>INFO<span style="font-family:宋体;">、</span>DEBUG<span style="font-family:宋体;">），设置输出目的地命名。</span></p><p><span style="font-family:Wingdings;">l<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>log4j.appender.appenderName<span style="font-family:宋体;">配置日志信息输出目的地</span>Appender</p><p>Appender <span style="font-family:宋体;">为日志输出目的地，</span>Log4j<span style="font-family:宋体;">提供的</span>appender<span style="font-family:宋体;">有以下几种：</span></p><p><span style="font-family:Wingdings;">n<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>org.apache.log4j.ConsoleAppender<span style="font-family:宋体;">（控制台）</span></p><p><span style="font-family:Wingdings;">n<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>org.apache.log4j.FileAppender<span style="font-family:宋体;">（文件）</span></p><p><span style="font-family:Wingdings;">n<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>org.apache.log4j.DailyRollingFileAppender<span style="font-family:宋体;">（每天产生一个日志文件）</span></p><p><span style="font-family:Wingdings;">n<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>org.apache.log4j.RollingFileAppender<span style="font-family:宋体;">（文件大小到达指定尺寸时产生一个新的文件）</span></p><p><span style="font-family:Wingdings;">n<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>org.apache.log4j.WriterAppender<span style="font-family:宋体;">（将日志信息以流格式发送到任意指定的地方）</span></p><p><span style="font-family:Wingdings;">l<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>Layout<span style="font-family:宋体;">配置日志输出格式</span></p><p>Log4j<span style="font-family:宋体;">提供的</span>layout<span style="font-family:宋体;">有以下几种：</span></p><p><span style="font-family:Wingdings;">n<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>org.apache.log4j.HTMLLayout<span style="font-family:宋体;">（以</span>HTML<span style="font-family:宋体;">表格形式布局），</span></p><p><span style="font-family:Wingdings;">n<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>org.apache.log4j.PatternLayout<span style="font-family:宋体;">（可以灵活地指定布局模式），</span></p><p><span style="font-family:Wingdings;">n<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>org.apache.log4j.SimpleLayout<span style="font-family:宋体;">（包含日志信息的级别和信息字符串），</span></p><p><span style="font-family:Wingdings;">n<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>org.apache.log4j.TTCCLayout<span style="font-family:宋体;">（包含日志产生的时间、线程、类别等等信息）</span></p><p><span style="font-family:Wingdings;">l<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>ConversionPattern<span style="font-family:宋体;">配置打印参数</span></p><p>Log4J<span style="font-family:宋体;">采用类似</span>C<span style="font-family:宋体;">语言中的</span>printf<span style="font-family:宋体;">函数的打印格式格式化日志信息，如表</span>3-7<span style="font-family:宋体;">所示。</span></p><p><span style="font-family:黑体;">表</span>3-7 log4j<span style="font-family:黑体;">日志输出打印格式</span></p><table border="1" cellspacing="0" cellpadding="0" width="520"> <tbody><tr><td style="width:40.85pt;"><p><span style="font-family:黑体;">格式</span></p></td><td style="width:348.95pt;"><p><span style="font-family:黑体;">说</span>&nbsp; <span style="font-family:黑体;">明</span></p></td></tr><tr><td style="width:40.85pt;"><p>%m</p></td><td style="width:348.95pt;"><p><span style="font-family:宋体;">输出代码中指定的消息</span></p></td></tr><tr><td style="width:40.85pt;"><p>%p</p></td><td style="width:348.95pt;"><p><span style="font-family:宋体;">输出优先级，即</span>DEBUG<span style="font-family:宋体;">，</span>INFO<span style="font-family:宋体;">，</span>WARN<span style="font-family:宋体;">，</span>ERROR<span style="font-family:宋体;">，</span>FATAL</p></td></tr><tr><td style="width:40.85pt;"><p>%r</p></td><td style="width:348.95pt;"><p><span style="font-family:宋体;">输出自应用启动到输出该</span>log<span style="font-family:宋体;">信息耗费的毫秒数</span></p></td></tr><tr><td style="width:40.85pt;"><p>%c</p></td><td style="width:348.95pt;"><p><span style="font-family:宋体;">输出所属的类目，通常就是所在类的全名</span></p></td></tr><tr><td style="width:40.85pt;"><p>%t</p></td><td style="width:348.95pt;"><p><span style="font-family:宋体;">输出产生该日志事件的线程名</span></p></td></tr><tr><td style="width:40.85pt;"><p>%n</p></td><td style="width:348.95pt;"><p><span style="font-family:宋体;">输出一个回车换行符，</span>Windows<span style="font-family:宋体;">平台为“</span>/r/n<span style="font-family:宋体;">”，</span>Unix<span style="font-family:宋体;">平台为“</span>/n<span style="font-family:宋体;">”</span></p></td></tr><tr><td style="width:40.85pt;"><p>%d</p></td><td style="width:348.95pt;"><p><span style="font-family:宋体;">输出日志时间点的日期或时间，默认格式为</span>ISO8601<span style="font-family:宋体;">，也可以在其后指定格式，比如：</span>%d{yyy MMM dd HH:mm:ss , SSS}<span style="font-family:宋体;">，输出类似：</span>2002<span style="font-family:宋体;">年</span>10<span style="font-family:宋体;">月</span>18<span style="font-family:宋体;">日</span>22:10:28<span style="font-family:宋体;">，</span>921</p></td></tr><tr><td style="width:40.85pt;"><p>%l</p></td><td style="width:348.95pt;"><p><span style="font-family:宋体;">输出日志事件的发生位置，包括类目名、发生的线程，以及在代码中的行数。</span></p></td></tr></tbody></table><p><span style="font-family:宋体;">作为最核心的模块之一，</span>Nutz<span style="font-family:宋体;">的日志信息已经相当完善，看懂</span>Nutz<span style="font-family:宋体;">的日志，也是了解</span>Nutz<span style="font-family:宋体;">工作方式的核心途径之一。从控制台</span>Console<span style="font-family:宋体;">可以看到很多的日志信息，与</span>Nutz<span style="font-family:宋体;">相关的，基本上包括以下内容：</span></p><p><span style="font-family:Wingdings;">l<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>NutFilter<span style="font-family:宋体;">启动</span></p><p><span style="font-family:Wingdings;">l<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span><span style="font-family:宋体;">资源扫描器开始初始化</span></p><p><span style="font-family:Wingdings;">l<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>NutFilter<span style="font-family:宋体;">输出容器信息</span></p><p><span style="font-family:Wingdings;">l<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span><span style="font-family:宋体;">加载</span>MainModule</p><p><span style="font-family:Wingdings;">l<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span><span style="font-family:宋体;">加载</span>Ioc<span style="font-family:宋体;">配置信息</span></p><p><span style="font-family:Wingdings;">l<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span><span style="font-family:宋体;">解析路径映射的信息</span></p><p><span style="font-family:Wingdings;">l<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span><span style="font-family:宋体;">执行用户自定义</span>Setup<span style="font-family:宋体;">的信息</span></p><p><span style="font-family:Wingdings;">l<span style="font-size:7pt;font-family:'Times New Roman';">&nbsp; </span></span>NutFilter<span style="font-family:宋体;">完成的耗时信息</span></p><p>&nbsp;</p><br /></div>  </td>
        </tr>

		</tbody>
    </table>
<div th:include="include :: footer"></div>
</body>
</html>
